const express= require('express');
let router = express.Router();
const common = require('../../libs/common');
let isLogin='';

// 不需要登录的路由
const notLogRouter = ['/','/header','/footer','/forgot','/getIndexData','/login','/logout','/captcha','/reg','/sendMail','/change','/favicon.ico']
router.use((req,res,next)=>{
     // 如果没有SESSION并且是需要登录的路由则 重定向到LOGIN页面
    if(!req.session['shoe-user_ID'] && !notLogRouter.includes(req.path)){
        console.log(1,req.path)
        isLogin = 0;
        res.redirect(`/?ref=${req.url}`);
    }else{
        if(notLogRouter.includes(req.path)){
            next()
        }else{
            req.db.query(`SELECT ID FROM user WHERE ID='${req.session['shoe-user_ID']}'`, (err, data) => {
                if(err){
                    res.sendStatus(500);
                }else if(data==0){
                    isLogin = 0;
                    res.redirect('/');
                }else{
                    req.client.get(req.session['shoe-user_ID'], function(err, reply){
                        if(!null){
                            console.log(reply)
                            if(req.session['shoe-token']!=reply){
                                req.session['shoe-user_ID']='';
                                isLogin = 0;
                                res.redirect('/');
                            }else{
                                next()
                            }
                        }
                    })
                }
            })
            
        }
    
    }
    // next()
})
router.get('/', (req, res) => {
    res.render('index');
})
router.get('/detail', (req, res) => {
    global.line = req.query['line'];
    global.row = req.query['row'];

    if(detailData[line]!=undefined && detailData[line][row]!=undefined){
        res.render('detail',{
            title:detailData[line][row]['title'] || '',
            price:detailData[line][row]['price'] || '0.00',
            img_src:detailData[line][row]['img_src'] || '',
        });
    }else{
        res.render('detail',{
            title:'',
            price:'0.00',
            img_src:'',
        });
    }
})
// 请求header
router.get('/header', (req, res,next) => {
    console.log(isLogin)
    // 如果没有SESSION则隐藏  账户信息
    if(!req.session['shoe-user_ID']){
        res.render('header',{
            login_btn_show:'0',
            email:'',
            isLogin:isLogin
        });
    }else{
        res.render('header',{
            login_btn_show:'1',
            email:req.session['shoe-email'],
            isLogin:isLogin
        });
    }
    isLogin='';
})

// 请求footer
router.get('/footer', (req, res) => {
    res.render('footer');
})
// 请求订单页
router.get('/order', (req, res) => {
    req.db.query(`SELECT ID,price,payment_mode,payment_time FROM pay_order WHERE user_id='${req.session['shoe-user_ID']}'`, (err, data) => {
        if (err) {
            res.sendStatus(500);
        } else {
            function getMode(num){
                if(num==0){
                    return {
                        font:'FUNPAY',
                        color:'#72d3d9'
                    }
                }else if(num==1){
                    return {
                        font:'VISA',
                        color:'#2eb031'
                    }
                }else if(num==2){
                    return {
                        font:'UNIONPAY',
                        color:'#e7c59f'
                    }
                }
            }
            let orderData=[];
            function NumAscSort(a,b){
                return b.payment_time - a.payment_time;
            }
            data.sort(NumAscSort)
            for(let i=0;i<data.length;i++){
                orderData.push({
                   id: data[i].ID,
                   price: common.returnFloat(data[i].price),
                   payment_mode: getMode(data[i].payment_mode).font,
                   color: getMode(data[i].payment_mode).color,
                   payment_time: common.getTime(data[i].payment_time,'MM/dd/yyyy hh:mm')
                })
            }
            res.render('order',{data:orderData});
        }
    })
})
// 退出登录
router.get('/logout', (req, res,next) => {
    req.session['shoe-user_ID']='';
    res.redirect('/');
})

// // 租房爬虫函数
// router.use('/house',require('./api/house'))
// // 红酒爬虫函数
// router.use('/wine',require('./api/wine'))
// // 化妆品爬虫函数
// router.use('/cosmetics',require('./api/cosmetics'))
// // 珠宝爬虫函数
// router.use('/jewelry',require('./api/jewelry'))
// // 服装爬虫函数
// 请求图形验证码
router.use('/captcha',require('./api/captcha'));
router.use('/login',require('./api/login'));
router.use('/reg',require('./api/reg'));
router.use('/sendMail',require('./api/sendMail'));
router.use('/forgot',require('./api/forgot'));
router.use('/change',require('./api/change'));
router.use('/pay',require('./api/pay'));

// 获取页面数据
router.use('/getIndexData',require('./reptile/clothing'));
module.exports = router;
